﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using AutoFacUtils;
using Common;
using CashFlow.Db.Entity;
using CashFlow.ShopManage.Validator.User;
using CashFlow.ShopManage.Service;
using CashFlow.ShopManage.Common;
using CashFlow.ShopManage.Def;
using CashFlow.ShopManage.Models;
using CashFlow.ShopManage.Models.User;
using CashFlow.ShopManage.Models.User.Result;
using CashFlow.ShopManage.Filters;
using CashFlow.ShopManage.Validator;

namespace CashFlow.ShopManage.Controllers.Shop
{
    public class UserController : Controller
    {
        private IAdminService GetAdminService() { return AutoFacHelper.Get<IAdminService>(); }
        private IUserService GetUserService() { return AutoFacHelper.Get<IUserService>(); }

        [HttpGet]
        public ActionResult Index()
        {
            var service = GetAdminService();
            var ui = service.GetLoginUserInfo();
            ViewBag.ShopManagerUserId = ui.ShopManagerUserId;
            return View();
        }

        [HttpPost]
        [ValidatorFilter(typeof(UserInfoQueryParamModelValidator))]
        public ActionResult List(UserInfoQueryParamModel model)
        {
            var adminService = GetAdminService();
            var userService = GetUserService();
            var ui = adminService.GetLoginUserInfo();
            var ls = userService.QueryShopAllUsersByShopManagerUserIdByWhere(
                        ui.ShopManagerUserId
                        , model.Phone
                        , model.Name
                        , model.InvitationCode
                        , model.Reference
                        , model.ReferenceInvitationCode
                        , model.State
                     );
            var ls2 = ls.Select(x => Convert(x)).ToList();
            return MyCommonTool.CreateLayUITableResult(true, "", ls2, ls2.Count);
        }

        [HttpPost]
        public ActionResult List2()
        {
            var adminService = GetAdminService();
            var userService = GetUserService();
            var ui = adminService.GetLoginUserInfo();
            var ls = userService.QueryShopAllUsersByShopManagerUserId2(
                        ui.ShopManagerUserId
                        , 1
                     );
            var ls2 = ls.Select(x => Convert(x)).ToList();
            return MyCommonTool.CreateOperationResult(true, "", ls2);
        }

        [HttpPost]
        [ValidatorFilter(typeof(UpdateUserModelValidator))]
        public ActionResult UpdateUser(UpdateUserModel model)
        {
            var userService = GetUserService();
            var result = userService.UpdateUser(
                            model.UserId
                            , model.Phone
                            , model.Name
                         );
            return MyCommonTool.CreateOperationResult(
                        result == EUpdateUserResult.Success
                        , EnumTool.GetDescription<EUpdateUserResult, int>(result)
                        , null
                   );
        }

        [HttpPost]
        [ValidatorFilter(typeof(ResetUserPwdModelValidator))]
        public ActionResult ResetPwd(ResetUserPwdModel model)
        {
            var userService = GetUserService();
            bool b = userService.ResetPwd(model.UserId, model.NewPwd);
            return MyCommonTool.CreateOperationResult(b, "", "");
        }

        [HttpPost]
        [ValidatorFilter(typeof(UserIdModelValidator))]
        public ActionResult QueryUserStructure(UserIdModel model)
        {
            var userService = GetUserService();
            var ls = userService.QueryShopAllUsersByUserId(model.UserId, null);
            return MyCommonTool.CreateOperationResult(true, "", ls);
        }

        [HttpPost]
        [ValidatorFilter(typeof(UpdateShopManagerLevelPercentModelValidator))]
        public ActionResult UpdateShopManagerLevelPercent(
            UpdateShopManagerLevelPercentModel model)
        {
            var service = GetUserService();
            bool b = service.UpdateShopManagerLevelPercent(model.UserId, model.Percent);
            return MyCommonTool.CreateOperationResult(b, "", "");
        }

        [HttpPost]
        [ValidatorFilter(typeof(UpdateUserStateModelValidator))]
        public ActionResult UpdateUserState(UpdateUserStateModel model)
        {
            var service = GetUserService();
            bool b = service.UpdateUserState(model.UserId, model.State);
            return MyCommonTool.CreateOperationResult(b, "", "");
        }

        [HttpPost]
        [ValidatorFilter(typeof(UpdateUserReferenceInviationCodeModelValidator))]
        public ActionResult UpdateUserReferenceInvitationCode(
            UpdateUserReferenceInviationCodeModel model)
        {
            var service = GetUserService();
            bool b = service.UpdateUserReferenceInvitationCode(model.UserId
                        , model.ReferenceInvitationCode
                     );
            return MyCommonTool.CreateOperationResult(b, "", null);
        }

        [HttpPost]
        [ValidatorFilter(typeof(InvitationCodeModelValidator))]
        public ActionResult QueryByReferenceInvitationCode(InvitationCodeModel model)
        {
            var adminService = GetAdminService();
            var ui = adminService.GetLoginUserInfo();
            var userService = GetUserService();
            var entity = userService.QueryByInvitationCode(
                            ui.ShopManagerUserId
                            , model.InvitationCode
                         );
            var entity2 = entity ?? new UserInfoEntity();
            var entity3 = Convert2(entity2);
            return MyCommonTool.CreateOperationResult(true, "", entity3);
        }

        private UserInfoQueryParamModelResult Convert(UserInfoAndReferenceUserInfo ur)
        {
            return new UserInfoQueryParamModelResult
            {
                Id = ur.UserInfo.Id,
                CreateTime = ur.UserInfo.CreateTime,
                InvitationCode = ur.UserInfo.InvitationCode,
                LoginTime = ur.UserInfo.LoginTime,
                Name = ur.UserInfo.Name,
                Phone = ur.UserInfo.Phone,
                ReferenceInvitationCode = ur.UserInfo.ReferenceInvitationCode,
                ReferenceName = ur.ReferenceUserInfo.Name,
                State = ur.UserInfo.State,
                ShopManagerLevel = ur.UserInfo.ShopManagerLevelPercent
            };
        }

        private UserInfoResultModel Convert2(UserInfoEntity entity)
        {
            return new UserInfoResultModel
            {
                Id = entity.Id,
                Name = entity.Name,
                InvitationCode = entity.InvitationCode,
                ReferenceInvitationCode = entity.ReferenceInvitationCode
            };
        }
    }
}